package Rozetka.Pages;

import Rozetka.Pages.BasePage;
import Rozetka.Pages.CheckOutPage;
import Rozetka.ReportWriter.ReportWriter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.NoSuchElementException;

public class SearchPage extends BasePage {

    private static By foundElement = By.xpath("/html/body/div/div[3]/div/div/div/div/div[3]/div/div[2]/div/a");
    private static By buyButton = By.name("topurchases");
    private static By sumbitOrder = By.id("popup-checkout");

    public SearchPage(WebDriver driver) {
        super(driver);
    }

    public boolean isSearchFoundSmoke () {
        ReportWriter.info("Check result");

        if (driver.findElement(By.className("search-result-goods")).isDisplayed()) {
            ReportWriter.info("Smoke search is done");
            return true;
        } else {
            ReportWriter.error("Smoke search is failed");
            return false;
        }
    }

    public CheckOutPage makeOrder(String amount) {
        ReportWriter.info("OnlineOrder");

        try {
            driver.findElement(foundElement).click();
            driver.findElement(buyButton).click();
            driver.findElement(By.name("quantity")).clear();
            driver.findElement(By.name("quantity")).sendKeys(amount);
            driver.findElement(sumbitOrder).click();
        } catch (NoSuchElementException e) {
            ReportWriter.error("Didn't find element");
        }

        return new CheckOutPage(driver);
    }
}
